import 'package:dio/dio.dart';
import 'http_service.dart';
import '../config/app_config.dart';

/// API 服务（对应 tzai-h5 的 API 层）
class ApiService {
  final HttpService _http = HttpService();
  
  // ========== 资讯相关 ==========
  
  /// 获取资讯列表
  Future<Response> getArticleList({
    int pageNum = 1,
    int pageSize = AppConfig.pageSize,
    int? companyId,
    String? articleType,
    String? status,
  }) async {
    return await _http.get('/tzai/article/list', queryParameters: {
      'pageNum': pageNum,
      'pageSize': pageSize,
      if (companyId != null) 'companyId': companyId,
      if (articleType != null) 'articleType': articleType,
      if (status != null) 'status': status,
    });
  }
  
  /// 获取资讯详情
  Future<Response> getArticleDetail(int articleId) async {
    return await _http.get('/tzai/article/$articleId');
  }
  
  /// 获取指定语言的内容
  Future<Response> getArticleContent(int articleId, String language) async {
    return await _http.get('/tzai/content/article/$articleId', queryParameters: {
      'language': language,
    });
  }
  
  /// 获取音频信息
  Future<Response> getAudioInfo(int articleId, String language) async {
    return await _http.get('/tzai/audio/article/$articleId', queryParameters: {
      'language': language,
    });
  }
  
  /// 播放音频（获取音频流）
  Future<Response> playAudio(int audioId, String savePath) async {
    return await _http.download(
      '/tzai/audio/play/$audioId',
      savePath: savePath,
      options: Options(
        responseType: ResponseType.bytes,
        receiveTimeout: const Duration(minutes: 3),
      ),
    );
  }
  
  // ========== 公司相关 ==========
  
  /// 获取公司列表
  Future<Response> getCompanyList() async {
    return await _http.get('/tzai/company/list');
  }
  
  /// 根据业务类型和业务ID查询文件（用于获取公司Logo）
  Future<Response> getFileByBiz(String bizType, int bizId) async {
    return await _http.get('/tzai/file/biz', queryParameters: {
      'bizType': bizType,
      'bizId': bizId,
    });
  }
  
  /// 获取文件查看URL
  String getFileViewUrl(int fileId) {
    return '${AppConfig.baseUrl}/tzai/file/view/$fileId';
  }
  
  // ========== 用户相关 ==========
  
  /// 检查是否已收藏
  Future<Response> checkFavorite({required int articleId}) async {
    return await _http.get('/tzai/favorite/check', queryParameters: {
      'articleId': articleId,
    });
  }
  
  /// 切换收藏状态（收藏/取消收藏），返回 isFavorited
  Future<Response> toggleFavorite({required int articleId}) async {
    return await _http.post('/tzai/favorite/toggle', queryParameters: {
      'articleId': articleId,
    });
  }
  
  /// 获取我的收藏列表
  Future<Response> getFavorites({
    int pageNum = 1,
    int pageSize = AppConfig.pageSize,
  }) async {
    return await _http.get('/tzai/favorite/list', queryParameters: {
      'pageNum': pageNum,
      'pageSize': pageSize,
    });
  }
  
  /// 添加收藏
  Future<Response> addFavorite(int articleId) async {
    return await _http.post('/tzai/user/favorites', data: {
      'articleId': articleId,
    });
  }
  
  /// 取消收藏
  Future<Response> removeFavorite(int articleId) async {
    return await _http.delete('/tzai/user/favorites/$articleId');
  }
  
  /// 获取播放历史
  Future<Response> getPlayHistory({
    int pageNum = 1,
    int pageSize = AppConfig.pageSize,
  }) async {
    return await _http.get('/tzai/user/history', queryParameters: {
      'pageNum': pageNum,
      'pageSize': pageSize,
    });
  }
  
  /// 记录播放历史
  Future<Response> recordPlayHistory(Map<String, dynamic> data) async {
    return await _http.post('/tzai/user/history', data: data);
  }
  
  /// 获取用户设置
  Future<Response> getUserSettings() async {
    return await _http.get('/tzai/user/settings');
  }
  
  /// 更新用户设置
  Future<Response> updateUserSettings(Map<String, dynamic> data) async {
    return await _http.put('/tzai/user/settings', data: data);
  }
  
  /// 获取用户统计
  Future<Response> getUserStats() async {
    return await _http.get('/tzai/user/stats');
  }
  
  // ========== 认证相关 ==========
  
  /// 登录
  Future<Response> login(String username, String password) async {
    return await _http.post('/login', data: {
      'username': username,
      'password': password,
    });
  }
  
  /// 登出
  Future<Response> logout() async {
    return await _http.post('/logout');
  }
}

